package org.x.user.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.common.config.BusinessException;
import org.x.user.dto.DataScopeDTO;
import org.x.user.entity.SysRoleDeptEntity;
import org.x.user.mapper.SysDeptMapper;
import org.x.user.mapper.SysRoleDeptMapper;
import org.x.user.mapper.SysRoleMapper;
import org.x.user.service.ISysRoleDeptService;
import org.x.user.entity.SysRoleEntity;
import java.util.List;

@Service
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptMapper,SysRoleDeptEntity> implements ISysRoleDeptService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;

    @Override
    public int dataScope(DataScopeDTO dataScopeDTO) {
        //校验角色存在性
        if(sysRoleMapper.selectOneById(dataScopeDTO.getRoleId()) == null){
            throw new BusinessException("角色不存在");
        }

        int count = 0;
        //校验数据权限范围(自定义部门ID列表（dataScope=3时必填）)
        switch (dataScopeDTO.getDataScope()){
            case 1:
                SysRoleEntity sysRoleEntity1 = sysRoleMapper.selectOneById(dataScopeDTO.getRoleId());
                if(sysRoleEntity1.getDataScope() == 1){
                    throw new BusinessException("角色数据权限范围已经为全部");
                }
                sysRoleEntity1.setDataScope(1);
                sysRoleMapper.update(sysRoleEntity1);

                SysRoleDeptEntity sysRoleDeptEntity1 = new SysRoleDeptEntity();
                sysRoleDeptEntity1.setRoleId(dataScopeDTO.getRoleId());
                //获取数据库中所有部门id
                List<Long> dbDeptIds = sysRoleDeptMapper.selectListByQuery(new QueryWrapper())
                        .stream()
                        .map(SysRoleDeptEntity::getDeptId)
                        .toList();
                for (Long deptId:dbDeptIds){
                    sysRoleDeptEntity1.setDeptId(deptId);
                    sysRoleDeptMapper.insert(sysRoleDeptEntity1);
                    count++;
                }
                return count;
            case 2:
                SysRoleEntity sysRoleEntity2 = sysRoleMapper.selectOneById(dataScopeDTO.getRoleId());
                if(sysRoleEntity2.getDataScope() == 2){
                    throw new BusinessException("角色数据权限范围已经为本部门");
                }
                sysRoleEntity2.setDataScope(2);
                sysRoleMapper.update(sysRoleEntity2);
                return 1;
            case 3:
                if(dataScopeDTO.getDeptIds() == null || dataScopeDTO.getDeptIds().length == 0) {
                    throw new BusinessException("数据权限范围（自定义部门ID列表）不能为空");
                } else {
                    for(Long deptId : dataScopeDTO.getDeptIds()) {
                        if(sysDeptMapper.selectOneById(deptId) == null) {
                            throw new BusinessException("部门不存在");
                        }
                    }

                    SysRoleEntity sysRoleEntity = sysRoleMapper.selectOneById(dataScopeDTO.getRoleId());
                    if(sysRoleEntity.getDataScope() == 3) {
                        throw new BusinessException("角色数据权限范围已经为自定义部门");
                    }
                    sysRoleEntity.setDataScope(3);
                    sysRoleMapper.update(sysRoleEntity);

                    SysRoleDeptEntity sysRoleDeptEntity = new SysRoleDeptEntity();
                    sysRoleDeptEntity.setRoleId(dataScopeDTO.getRoleId());
                    for(Long deptId:dataScopeDTO.getDeptIds()){
                        sysRoleDeptEntity.setDeptId(deptId);
                        sysRoleDeptMapper.insert(sysRoleDeptEntity);
                        count++;
                    }
                    return count;
                }
        }
        return 0;
    }
}
